/*密码加密工具*/
import CryptoJS from 'crypto-js'

/**
 * AES加密 ：字符串 key iv  返回base64
 */
export function encrypt(word) {
    let key = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_AES_KEY);
    let iv = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_AES_IV);

    let srcs = CryptoJS.enc.Utf8.parse(word);
    var encrypted = CryptoJS.AES.encrypt(srcs, key, {
        iv: iv,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.ZeroPadding
    });
    return CryptoJS.enc.Base64.stringify(encrypted.ciphertext);

}
/**
 * AES 解密 ：字符串 key iv  返回base64
 *
 * @return {string}
 */
export function decrypt(word) {
    let key = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_AES_KEY);
    let iv = CryptoJS.enc.Utf8.parse(import.meta.env.VITE_AES_IV);

    let base64 = CryptoJS.enc.Base64.parse(word);
    let src = CryptoJS.enc.Base64.stringify(base64);
    let decrypt=''
    try {
        decrypt = CryptoJS.AES.decrypt(src, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.ZeroPadding
        }); 
        let decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
        return decryptedStr.toString();
    } catch (error) {
        return word
    }
}